﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S4Store.Domain.NetMarketingKpi;
using S4Store.Domain.NetMarketingKpiReport;

namespace S4Store.Service
{
    public class NetMarketingKpiReportService : BaseService
    {
        //public NetMarketingKpiReport GetNetMarketingKpiReport(int year, int month, int storeid)
        //{
        //    NetMarketingKpiService NetMarketingKpiService = new Service.NetMarketingKpiService();
        //    this.GetTranstion().Inject(NetMarketingKpiService);
        //    NetMarketingKpiReport report = new NetMarketingKpiReport();
        //    report.NetMarketingStandard = NetMarketingKpiService.GetNetMarketingStandard(year, month, storeid);
        //    report.NetMarketingMonthData = NetMarketingKpiService.GetNetMarketingMonthData(year, month, storeid, true);
        //    if (report.NetMarketingMonthData == null)
        //    {
        //        report.NetMarketingMonthData = new NetMarketingMonthData
        //        {
        //             Month=new DateTime(year,month,1),
        //              StoreId=storeid
        //        };
        //    }
        //    DateTime dt = new DateTime(year, month, 1).MonthLastOrNow();
        //    report.Year = dt.Year;
        //    report.Month = dt.Month;
        //    report.YearReport = new NetMarketingKpiYearReport();
        //    report.YearReport.Year = year;
        //    report.YearReport.NetMarketingMonthDatas=NetMarketingKpiService.GetNetMarketingMonthDatas(year, storeid, true).ToList();
        //    return report;
        //}

        public NetMarketingStoreReport GetStoreReport(int year, int month, int storeid)
        {
            var helper = getHelper(year, month, storeid);
            return helper.StoreReport;
        }

        public NetMarketingMediaReport GetMediaReport(int year, int month, int storeid, int mediaid,bool needlast=true)
        {
            var helper = getHelper(year, month, storeid, needlast);
            var result = helper.MediaReports.FirstOrDefault(t => t.Media.Id == mediaid);
            if (result == null)
            {
                DateTime dt = new DateTime(year, month, 1).MonthLastOrNow();
                NetMarketingKpiService NetMarketingKpiService = new NetMarketingKpiService();
                StoreService StoreService = new StoreService();
                this.GetTranstion().Inject(NetMarketingKpiService);
                this.GetTranstion().Inject(StoreService);

                result = new NetMarketingMediaReport
                {
                    Year = year,
                    Month = month,
                    Day = dt.Day,
                    Media = NetMarketingKpiService.GetMediaQuery().FirstOrDefault(x => x.Id == mediaid),
                    DayDatas = new List<NetMarketingKpiDayData>(),
                    LastMonthReport = new NetMarketingMediaReport(),
                   NetMarketingKpiMonthConfig= NetMarketingKpiService.GetMonthConfig(year, month, storeid, true),
                    MediaMonthConfig = NetMarketingKpiService.GetMediaMonthConfig(year, month, storeid, mediaid, true),
                    NetMarketingStandard = NetMarketingKpiService.GetNetMarketingStandard(year, month, storeid),
                    StoreReport = helper.StoreReport,
                    Store = StoreService.GetQuery().FirstOrDefault(t => t.Id == storeid)
                };
            }
            return result;
        }

        private Helper getHelper(int year, int month, int storeid, bool needlast = true)
        {
            Helper helper = new Helper();
            NetMarketingKpiService NetMarketingKpiService = new NetMarketingKpiService();
            StoreService StoreService = new StoreService();
            this.GetTranstion().Inject(NetMarketingKpiService);
            this.GetTranstion().Inject(StoreService);

            var daydatas = NetMarketingKpiService.GetDayDatas(year, month, storeid, true).ToList();
            var storeMonthConfig = NetMarketingKpiService.GetMonthConfig(year, month, storeid, true);
            if (storeMonthConfig == null)
            {
                storeMonthConfig =new NetMarketingKpiMonthConfig
                {
                    Month = new DateTime(year, month, 1),
                    StoreId = storeid
                };
            }
            var modelconfigs = NetMarketingKpiService.GetMediaMonthConfigs(year, month, storeid, true).ToList();
            var store = StoreService.GetQuery().FirstOrDefault(t => t.Id == storeid);
            var stand = NetMarketingKpiService.GetNetMarketingStandard(year, month, storeid);
            DateTime dt = new DateTime(year, month, 1).MonthLastOrNow();
            helper.StoreReport = new NetMarketingStoreReport
            {
                Year = dt.Year,
                Month = dt.Month,
                Day = dt.Day,
                Store = store,
                NetMarketingKpiMonthConfig = storeMonthConfig,
                MediaReports = new List<NetMarketingMediaReport>(),
                NetMarketingStandard = stand
            };

            var medias = NetMarketingKpiService.GetMediaQuery().ToList();
            List<NetMarketingMediaReport> reports = new List<NetMarketingMediaReport>();
            foreach (var media in medias)
            {
                NetMarketingMediaReport report = new NetMarketingMediaReport();
                report.Year = year;
                report.Month = month;
                report.Day = dt.Day;
                report.Media = media;
                report.NetMarketingKpiMonthConfig = storeMonthConfig;
                report.MediaMonthConfig = modelconfigs.FirstOrDefault(t => t.MediaId == media.Id);
                report.NetMarketingStandard = stand;
                if (report.MediaMonthConfig == null)
                {
                    report.MediaMonthConfig =new NetMarketingKpiMediaMonthConfig
                    {
                         MediaId=media.Id,
                          Media= media,
                        Store = store,
                        StoreId = storeid,
                        Month = new DateTime(year, month, report.Day)
                    };
                }
                report.StoreReport = helper.StoreReport;
                report.DayDatas = daydatas.Where(t => t.MediaId == media.Id).ToList();

                DateTime dtlast = new DateTime(year, month, report.Day).AddMonths(-1);
                if (needlast)
                {
                    report.LastMonthReport = GetMediaReport(dtlast.Year, dtlast.Month, storeid, media.Id,false);
                }
                else
                {
                    report.LastMonthReport = new NetMarketingMediaReport();
                }

                helper.StoreReport.MediaReports.Add(report);
                helper.MediaReports.Add(report);
            }
            return helper;
        }


        private class Helper
        {
            public Helper()
            {
                MediaReports = new List<NetMarketingMediaReport>();
            }
            public NetMarketingStoreReport StoreReport { get; set; }
            public List<NetMarketingMediaReport> MediaReports { get; set; }
        }
    }
}
